package com.yb.web.type;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;

import com.opensymphony.xwork2.ActionContext;
import com.yb.base.web.BaseAction;
import com.yb.bussiness.global.SystemLogService;
import com.yb.bussiness.global.SystemLogServiceImpl;
import com.yb.bussiness.goods.CategoryService;
import com.yb.bussiness.goods.CategoryServiceImpl;
import com.yb.bussiness.goods.TBrandTypeService;
import com.yb.bussiness.goods.TBrandTypeServiceImpl;
import com.yb.bussiness.type.TypeService;
import com.yb.bussiness.type.TypeServiceImpl;
import com.yb.domain.goods.TBrandType;
import com.yb.domain.goods.TgBrand;
import com.yb.domain.goods.TgCategory;
import com.yb.domain.goods.TgType;
import com.yb.util.comm.Contants;
import com.yb.util.comm.RequestUtil;

public class TypeAction extends BaseAction {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private TgType type;
	private TgBrand brand;
	private TgCategory cat;
	private List<TgCategory> catrows;
	private TypeService service;
	private CategoryService categoryService;
	private int total;
	private List rows;
	private List<TgCategory> catList;
	private String categoryId;
	private TBrandTypeService brandService;
	private TypeService typeService;
	private SystemLogService systemLogService;



	public TypeAction() {
		service = new TypeServiceImpl();
		categoryService = new CategoryServiceImpl();
		brandService = new TBrandTypeServiceImpl();
		typeService = new TypeServiceImpl();
		systemLogService = new SystemLogServiceImpl();
	}

	/**
	 * 跳转新增页面
	 * 
	 * @return
	 */
	public String addTypePage() {
		try {
			type = new TgType();
			type.setId(Long.valueOf(0));
			try {
				rows = categoryService.findAll(1, 50);
			} catch (Exception e) {
				log.error("显示品牌产品分类错误", e);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "addTypePage";
	}

	/**
	 * 新增
	 * 
	 * @return
	 */
	public String addType() {
		try {
			type.setCategoryId(Long.valueOf(categoryId));
			if(null==type.getId()||0==type.getId())
				 systemLogService.saveSystemLog(request, "产品种类", "新增类别--"+type.getName());
			 else
				 systemLogService.saveSystemLog(request, "产品种类", "修改类别--"+type.getName());
			service.saveOrUpdate(type);
			catrows = categoryService.findAll(1, 50);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "success";
	}

	/**
	 * 新增
	 * 
	 * @return
	 */
	public String manageType() {
		try {
			catrows = categoryService.findAll(1, 50);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "manageType";
	}

	/**
	 * 查询
	 * 
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String search() {
		menuCls = RequestUtil.getParam(request, "menuCls", "");
		Map<String, String> map = new HashMap<String, String>();
		String page = RequestUtil.getParam(request, "page", "1");
		String row = RequestUtil.getParam(request, "rows", "10");
		String name = RequestUtil.getParam(request, "name", "");
		String categoryId = RequestUtil.getParam(request, "categoryId", "");
		map.put("name", name);
		map.put("categoryId", categoryId);
		int count = service.getCount(map);
		if (count != 0) {
			total = count;
			List<TgType> list = service.getList(map, page, row);
			rows = list;
		}
		return Contants.JSONSTRING_RESULT_NAME;
	}

	/**
	 * 根据id删除
	 * 
	 * @return
	 */
	public String deleteType() {
		String id = RequestUtil.getParam(request, "id", "");
		String idString = RequestUtil.getParam(request, "idString", "");
		boolean delFlag = true;
		if (!"".equals(id)) {
			delFlag = service.checkDel(new Long(id));
		}
		if (delFlag) {
			boolean isFlag = false;
			if (!"".equals(id)){
				systemLogService.saveSystemLog(request, "产品种类", "删除产品种类--"+service.getById(id).getName());
				isFlag = service.deleteById(id);
			}else{
				String[] delteid = idString.split("_");
				try {
					for (int i = 0; i < delteid.length; i++) {
						systemLogService.saveSystemLog(request, "产品种类", "删除产品种类--"+service.getById(delteid[i]).getName());
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
				isFlag = service.deleteByIdString(idString);
			}
			if (isFlag)
				message = "删除成功";
			else
				message = "删除失败";
		}else{
			message = "该种类下拥有商家等信息，禁止删除";
		}
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = null;
		StringBuffer sb = new StringBuffer("[");
		sb.append("{\"message\":\"" + message + "\"" + "}");
		sb.append("]");
		try {
			out = response.getWriter();
			out.println(sb.toString());
		} catch (IOException e) {

			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 根据id查询
	 * 
	 * @return
	 */
	public String searchById() {
		String id = RequestUtil.getParam(request, "id", "");
		type = service.getById(id);
		List<TgCategory> pList = categoryService.findAll(1, 50);
		TgCategory tc = categoryService.findById(type.getCategoryId());
		List<TgCategory> cList = categoryService.findByPid(
				new Long(tc.getPid()), -1, -1);
		List<TgType> typeList = typeService.findByCategoryId(type
				.getCategoryId());
		List<TBrandType> btList = brandService.findByBIdAndCid(type.getId(),
				type.getCategoryId());
		ActionContext.getContext().put("pList", pList);
		ActionContext.getContext().put("cList", cList);
		ActionContext.getContext().put("typeList", typeList);
		ActionContext.getContext().put("btList", btList);
		ActionContext.getContext().put("bid", id);
		ActionContext.getContext().put("pid", tc.getPid());
		ActionContext.getContext().put("cid", type.getCategoryId());
		return "modTypePage";
	}

	public TgType getType() {
		return type;
	}

	public void setType(TgType type) {
		this.type = type;
	}

	/**
	 * 得到类别、品牌下拉
	 * 
	 * @return
	 */
	public String bulidSelect() {
		response.setContentType("text/html;charset=utf-8");
		Integer id = RequestUtil.getParam(request, "id", 0);
		PrintWriter out = null;
		catList = categoryService.findByPid(Long.valueOf(id), -1, -1);
		message = "<select id='brandselect' name='categoryId'>";
		if (null != catList && catList.size() > 0) {
			message += "<option value='" + id + "'>--</option>";
			for (int i = 0; i < catList.size(); i++) {
				message += "<option value='" + catList.get(i).getId() + "'>"
						+ catList.get(i).getName() + "</option>";
			}
		} else {
			message += "<option value='" + id + "'>--</option>";
		}
		message += "</select>";
		StringBuffer sb = new StringBuffer("[");
		sb.append("{\"message\":\"" + message + "\"" + "}");
		sb.append("]");
		try {
			out = response.getWriter();
			out.println(sb.toString());
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	public String loadTypeByCategoryId() {
		try {
			rows = service.findByCategoryId(Long.valueOf(categoryId));
			response.getWriter().print(JSONArray.fromObject(rows));
		} catch (Exception e) {
			log.error("根据种类id查找相应类别集合出错", e);
			try {
				response.getWriter().print("[]");
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		}
		return null;
	}

	public TgBrand getBrand() {
		return brand;
	}

	public void setBrand(TgBrand brand) {
		this.brand = brand;
	}

	public int getTotal() {
		return total;
	}

	public void setTotal(int total) {
		this.total = total;
	}

	public List getRows() {
		return rows;
	}

	public void setRows(List rows) {
		this.rows = rows;
	}

	public TgCategory getCat() {
		return cat;
	}

	public void setCat(TgCategory cat) {
		this.cat = cat;
	}

	public List<TgCategory> getCatList() {
		return catList;
	}

	public void setCatList(List<TgCategory> catList) {
		this.catList = catList;
	}

	public String getCategoryId() {
		return categoryId;
	}

	public void setCategoryId(String categoryId) {
		this.categoryId = categoryId;
	}

	public List<TgCategory> getCatrows() {
		return catrows;
	}

	public void setCatrows(List<TgCategory> catrows) {
		this.catrows = catrows;
	}
}
